package cn.xd.base.persistence.repository.impl;


import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import cn.xd.base.basic.type.AdminUserId;
import cn.xd.base.domain.entity.AdminUser;
import cn.xd.base.domain.entity.AdminUserOrgRole;
import cn.xd.base.domain.repository.AdminUserRepository;
import cn.xd.base.persistence.dos.AdminUserConverter;
import cn.xd.base.persistence.dos.AdminUserDO;
import cn.xd.base.persistence.dos.AdminUserOrgRoleConverter;
import cn.xd.base.persistence.dos.AdminUserOrgRoleDO;
import cn.xd.base.persistence.mps.*;
import cn.xd.core.ddd.db.DbRepositorySupport;
import cn.xd.core.ddd.diff.EntityDiff;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class AdminUserRepositoryImpl extends DbRepositorySupport<AdminUser, AdminUserId> implements AdminUserRepository {
    @Resource
    private AdminUserMpService adminUserMpService;
    @Resource
    private AdminUserOrgRoleMpService adminUserOrgRoleMpService;
    @Resource
    private GlobalAppUserMpService globalAppUserMpService;

    @Override
    protected void onInsert(AdminUser aggregate) {
        adminUserMpService.getAdminUserByAccount(aggregate.getAccount().getValue(), null).isNotEmptyThrow("该账号已经存在");
        if (ObjUtil.isNotEmpty(aggregate.getGlobalUserId().getValue())) {
            globalAppUserMpService.getResult(aggregate.getGlobalUserId()).isEmptyThrow("未找到该app用户");
            adminUserMpService.getAdminUserByGlobalUserId(aggregate.getGlobalUserId().getValue(), null).isNotEmptyThrow("该appUser已经被其他账号绑定");
        }
        adminUserMpService.saveResult(aggregate, AdminUserConverter.INSTANCE).successAndFailThrow((item) -> AddAssociatedData(aggregate, item));
    }

    @Override
    protected AdminUser onSelect(AdminUserId id) {
        AdminUser adminUser = adminUserMpService.getResult(id, AdminUserConverter.INSTANCE).get();
        if (ObjUtil.isNull(adminUser)) {
            return adminUser;
        }
        List<AdminUserOrgRoleDO> adminUserOrgRoles = (List<AdminUserOrgRoleDO>) adminUserOrgRoleMpService.getAdminUserOrgRoles(adminUser.getId().getValue()).get();
        adminUser.setOrgRoles(AdminUserOrgRoleConverter.INSTANCE.fromData(adminUserOrgRoles));
        return adminUser;
    }

    @Override
    protected void onUpdate(AdminUser aggregate, EntityDiff diff) {
        if (diff.isSelfModified()) {
            AdminUserDO bean = diff.getBean(AdminUserDO.class);
            if (StrUtil.isNotBlank(bean.getAccount())) {
                adminUserMpService.getAdminUserByAccount(aggregate.getAccount().getValue(), aggregate.getId().getValue()).isNotEmptyThrow("该账号已经存在");
            }
            if (ObjUtil.isNotEmpty(bean.getGlobalUserId())) {
                globalAppUserMpService.getResult(aggregate.getGlobalUserId()).isEmptyThrow("未找到该app用户");
                adminUserMpService.getAdminUserByGlobalUserId(aggregate.getGlobalUserId().getValue(), aggregate.getId().getValue()).isNotEmptyThrow("该appUser已经被其他账号绑定");
            }

            adminUserMpService.updateResult(bean);
        }
        updateList(AdminUser::getOrgRoles, diff, AdminUserOrgRoleConverter.INSTANCE);
    }


    @Override
    protected void onDelete(AdminUser aggregate) {
        aggregate.delete();
        AdminUserDO adminUserDO = AdminUserConverter.INSTANCE.toData(aggregate);
        adminUserMpService.updateResult(adminUserDO);
    }

    /**
     * 新增关联数据
     *
     * @param aggregate
     * @param bean
     */
    private void AddAssociatedData(AdminUser aggregate, AdminUserDO bean) {
        for (AdminUserOrgRole adminOrg : aggregate.getOrgRoles()) {
            adminOrg.setUserId(AdminUserId.of(bean.getId()));
            adminUserOrgRoleMpService.saveResult(adminOrg, AdminUserOrgRoleConverter.INSTANCE);
        }
    }
}
